<?php

/**
 *  Description of Register
 *
 * @author ciro cir.han@shangbox.com
 * @copyright 2012-2013 shangbox.com
 * $Id: User.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @history    2012-9-28  ::  ciro  ::  Create File
 */
class Model_User extends Model_Abstract
{

    protected $db;

    function __construct()
    {
        $this->db = Com_DB::get('BingoMobi');
        $this->_session = Core_Session::getInstance();
    }

    /**
     * 用户注册信息添加
     * @param array $data
     * @return true false
     */
    public function addUser($data)
    {
        $ok = 0;
        $setArr = array(
            "uuid" => "{$data["uuid"]}",
            "email" => "{$data["email"]}",
            "passwd" => "{$data["passwd"]}",
            "type" => "{$data["type"]}",
            "name" => "{$data["name"]}",
            "phone" => "{$data["phone"]}",
            "firm" => "{$data["firm"]}",
            "reg_ip" => "{$data["reg_ip"]}",
            "login_ip" => "{$data["login_ip"]}",
            "create_time" => "{$data["create_time"]}",
            "login_time" => "{$data["login_time"]}",
            "active" => "0"
        );
        $ok = $this->db->insert('bg_global_user', $setArr);
        return $ok;
    }

    /**
     * 用户基本信息更新
     * @param type $data
     */
    function updateUser($data)
    {
        $ok = 0;
        $data["login_time"] = time();
        $setArr = array(
            "firm" => "{$data["firm"]}",
            "qq" => "{$data["qq"]}",
            "address" => "{$data["address"]}",
            "zipcode" => "{$data["zipcode"]}",
            "content" => "{$data["content"]}",
            "login_time" => "{$data["login_time"]}"
        );
        $ok = $this->db->update("bg_global_user", $setArr, "id={$data["id"]}");
        if ($ok) {
            $usr = $this->_session["UserInfo"];
            foreach ($setArr as $k => $v) {
                $usr[$k] = $v;
            }
            $this->_session["UserInfo"] = $usr;
        }
        return $ok;
    }

    /**
     * 密码更新
     * @param type $data
     * @return int
     */
    function updatePass($data)
    {
        $ok = "密码更新成功";
        $data["login_time"] = time();
        $setArr = array(
            "passwd" => "{$data["pass"]}"
        );
        $ms = $this->db->update("bg_global_user", $setArr, "id={$data["id"]}");
        if ($ms) {
            $usr = $this->_session["UserInfo"];
            foreach ($setArr as $k => $v) {
                $usr[$k] = $v;
            }
            $this->_session["UserInfo"] = $usr;
        } else {
            $ok = "密码更新失败";
        }
        return $ok;
    }
    /**
     * 判断邮箱是否注册
     * @param type $email
     * @return $row
     */
    public function checkEmail($email)
    {
        $row = $this->db->fetchRow("SELECT id from bg_global_user WHERE email='{$email}'");
        return $row;
    }
    /**
     * 获取用户信息列表
     * @param type $star
     * @param type $estar
     * @param type $wheresql
     * @param type $seachtype
     * @param type $type
     * @return type
     */
    function getWebUserList($star, $estar, $wheresql, $seachtype, $type = 0)
    {
        $where = " ";
        switch ($seachtype) {
            case "1":  //应用首页搜索
                $sql = " AND 1 ";
                if (!empty($wheresql["username"])) {
                    $where = " AND email LIKE '%" . $wheresql["username"] . "%' ";
                }
                break;
            default:
                $sql = " ";
        }
        return $this->db->limitQuery("SELECT * FROM bg_global_user WHERE 1{$where}AND active={$type}", $star, $estar);
    }
    /**
     * 获取用户列表信息
     * @param type $star
     * @param type $estar
     * @param type $wheresql
     * @param type $seachtype
     * @param type $type
     * @return type
     */
    function getSysUserList($star, $estar, $wheresql, $seachtype, $type = 1){
               $where = " ";
        switch ($seachtype) {
            case "1":  //应用首页搜索
                $sql = " AND 1 ";
                if (!empty($wheresql["assname"])) {
                    $where = " AND admin_name LIKE '%" . $wheresql["assname"] . "%' ";
                }
                if (!empty($wheresql["assid"])) {
                    $where = " AND admin_id LIKE '%" . $wheresql["assid"] . "%' ";
                }
                break;
            default:
                $sql = " ";
        }
        return $this->db->limitQuery("SELECT * FROM bg_sys_admin WHERE 1{$where}AND status={$type}", $star, $estar);
    }
    /**
     * 获取用户信息
     * @param type $id
     * @param type $type 1 webuser  2 sysuser
     * @return array
     */
    function getUserInfo($id,$type=1){
        $row=array();
        switch ($type){
            case "1":
                $wheresql="SELECT * FROM bg_global_user WHERE id='{$id}'";
                break;
            case "2":
                $wheresql="SELECT * FROM bg_sys_admin WHERE admin_id='{$id}'";
                break;
            default :
                $wheresql="SELECT * FROM bg_global_user WHERE id='{$id}'";
        }
        $row = $this->db->fetchRow($wheresql);
        return $row;
    }
    /**
     * 输入数据检测
     * @param type $data
     * @return boolean
     */
    public function checkInfo($data)
    {
        $ok = true;
        //邮箱验证
        $check = new Com_Validate();
        $ds = $this->checkEmail($data["email"]);
        if (isset($ds["id"])) {
            $ok = false;
        }
        if ($check->email($data["email"]) == false || $check->mobile($data["phone"]) == false) {
            $ok = false;
        }
        return $ok;
    }

}

?>
